<template>
  <div class="chat-bg">
    <div class="chat-layout">
      <aside class="sidebar">
        <div class="sidebar-header">
          <img src="/logo.png" class="logo" alt="logo" />
          <span class="brand">AI智周万物</span>
        </div>
        <el-button class="start-btn" type="primary" round icon="el-icon-plus" @click="startNewChat">开启新对话</el-button>
        <div class="menu-group">
          <div class="menu-title">智能助手</div>
          <div class="menu-item active"><i class="el-icon-chat-dot-round"></i> AI聊天</div>
          <div class="menu-item"><i class="el-icon-picture"></i> AI绘画</div>
          <div class="menu-item"><i class="el-icon-edit"></i> 作文批改</div>
          <div class="menu-item"><i class="el-icon-box"></i> 创意中心</div>
        </div>
        <div class="menu-group history">
          <div class="menu-title">历史对话</div>
          <el-scrollbar class="history-list">
            <div v-for="item in chatHistory" :key="item.id" class="history-item" :class="{active: item.id === activeChatId}" @click="selectChat(item.id)">
              <i class="el-icon-message"></i>
              <span class="title">{{ item.title }}</span>
              <el-icon @click.stop="deleteChat(item.id)" class="delete-icon"><i-ep-delete /></el-icon>
            </div>
          </el-scrollbar>
        </div>
      </aside>
      <main class="main-content">
        <div class="main-card">
          <div v-if="activeChatId" class="chat-area">
            <ChatWindow :messages="currentMessages" @send="sendMessage" />
          </div>
          <div v-else class="welcome-area">
            <div class="welcome-title">您好，欢迎使用智周万物AI系统！</div>
            <div class="quick-group">
              <div class="quick-card" v-for="(group, idx) in quickGroups" :key="idx">
                <div class="quick-title"><i :class="group.icon"></i> {{ group.title }}</div>
                <div class="quick-list">
                  <el-tag v-for="(q, qidx) in group.questions" :key="qidx" class="q-tag" @click="quickAsk(q)">{{ q }}</el-tag>
                </div>
              </div>
            </div>
            <div class="try-ask">
              <span>试一试问我</span>
              <el-tag v-for="(q, idx) in tryQuestions" :key="idx" class="try-tag" @click="quickAsk(q)">{{ q }}</el-tag>
            </div>
          </div>
        </div>
        <div class="input-float" v-if="activeChatId">
          <ChatWindow :messages="currentMessages" @send="sendMessage" input-only />
        </div>
      </main>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import ChatWindow from '../components/ChatWindow.vue'
import { ElMessage } from 'element-plus'

const chatHistory = ref([
  { id: 1, title: '和AI的第一次对话', messages: [
    { role: 'user', content: '你好，AI！' },
    { role: 'ai', content: '你好，有什么可以帮您？' }
  ] },
  { id: 2, title: '工作助理', messages: [
    { role: 'user', content: '帮我写一份周报' },
    { role: 'ai', content: '好的，请问需要包含哪些内容？' }
  ] }
])
const activeChatId = ref(null)

const currentMessages = computed(() => {
  const chat = chatHistory.value.find(c => c.id === activeChatId.value)
  return chat ? chat.messages : []
})

const quickGroups = [
  { title: 'AI创作', icon: 'el-icon-edit-outline', questions: ['写一首歌赞美祖国', '写一篇3年级的小学作文300字', '帮我出三道小学五年级的数学应用题'] },
  { title: '智能编程', icon: 'el-icon-cpu', questions: ['请帮我写一个贪吃蛇小游戏', '写一个爬虫抓取天气数据', '帮我生成一个Python排序算法'] },
  { title: '工作生活助手', icon: 'el-icon-suitcase', questions: ['写一份工作计划', '生成一份会议纪要', '帮我写一份周报'] }
]
const tryQuestions = ['扮演专家讲讲我联系的关系', '讲讲西游记的故事', '写一个老板看的设计方案']

function startNewChat() {
  const newId = Date.now()
  chatHistory.value.unshift({ id: newId, title: '新对话', messages: [] })
  activeChatId.value = newId
}
function selectChat(id) {
  activeChatId.value = id
}
function deleteChat(id) {
  chatHistory.value = chatHistory.value.filter(c => c.id !== id)
  if (activeChatId.value === id) activeChatId.value = null
  ElMessage.success('已删除对话')
}
function sendMessage(msg) {
  const chat = chatHistory.value.find(c => c.id === activeChatId.value)
  if (chat) {
    chat.messages.push({ role: 'user', content: msg })
    setTimeout(() => {
      chat.messages.push({ role: 'ai', content: '（AI回复）' + msg })
    }, 1200)
  }
}
function quickAsk(q) {
  if (!activeChatId.value) startNewChat()
  sendMessage(q)
}
</script>

<style scoped>
.chat-bg {
  min-height: 100vh;
  background: linear-gradient(120deg, #eaf3ff 0%, #f7fbff 100%);
}
.chat-layout {
  display: flex;
  height: 100vh;
}
.sidebar {
  width: 270px;
  background: rgba(255,255,255,0.92);
  border-radius: 32px 0 0 32px;
  box-shadow: 0 4px 32px rgba(77,138,255,0.08);
  padding: 32px 0 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.sidebar-header {
  display: flex;
  align-items: center;
  margin-bottom: 32px;
}
.logo {
  width: 38px;
  margin-right: 10px;
}
.brand {
  font-size: 1.3rem;
  font-weight: bold;
  color: #4d8aff;
}
.start-btn {
  width: 180px;
  height: 44px;
  font-size: 1.1rem;
  border-radius: 22px;
  margin-bottom: 32px;
  background: linear-gradient(90deg, #4d8aff 0%, #a0cfff 100%);
  border: none;
}
.menu-group {
  width: 100%;
  margin-bottom: 18px;
}
.menu-title {
  font-size: 1rem;
  color: #a0cfff;
  font-weight: bold;
  margin: 0 0 8px 38px;
}
.menu-item {
  display: flex;
  align-items: center;
  padding: 10px 38px;
  font-size: 1.08rem;
  color: #7a8fa7;
  border-radius: 12px 0 0 12px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.menu-item.active, .menu-item:hover {
  background: #eaf3ff;
  color: #4d8aff;
}
.menu-item i {
  margin-right: 10px;
  font-size: 1.2rem;
}
.history {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.history-list {
  flex: 1;
  padding: 0 8px 0 16px;
  overflow-y: auto;
}
.history-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 6px;
  transition: background 0.2s;
  font-size: 1rem;
  color: #4d8aff;
  background: #f7fbff;
}
.history-item.active, .history-item:hover {
  background: #eaf3ff;
}
.history-item .title {
  flex: 1;
  margin-left: 8px;
}
.delete-icon {
  color: #a0cfff;
  font-size: 1.1rem;
  margin-left: 8px;
  cursor: pointer;
}
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 48px 0 0 0;
}
.main-card {
  width: 900px;
  min-height: 600px;
  background: #fff;
  border-radius: 24px;
  box-shadow: 0 4px 32px rgba(77,138,255,0.08);
  padding: 32px 40px 120px 40px;
  position: relative;
}
.welcome-area {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.welcome-title {
  font-size: 1.6rem;
  color: #4d8aff;
  font-weight: bold;
  margin-bottom: 32px;
}
.quick-group {
  display: flex;
  gap: 24px;
  margin-bottom: 32px;
}
.quick-card {
  background: #f7fbff;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(77,138,255,0.06);
  padding: 18px 24px;
  min-width: 220px;
}
.quick-title {
  font-size: 1.1rem;
  color: #4d8aff;
  font-weight: bold;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.quick-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.q-tag {
  background: #eaf3ff;
  color: #4d8aff;
  border: none;
  font-size: 1.05rem;
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
  margin-bottom: 4px;
  transition: background 0.2s, color 0.2s;
}
.q-tag:hover {
  background: #4d8aff;
  color: #fff;
}
.try-ask {
  margin-top: 18px;
  color: #7a8fa7;
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 12px;
}
.try-tag {
  background: #f7fbff;
  color: #4d8aff;
  border: none;
  font-size: 1rem;
  border-radius: 8px;
  padding: 6px 14px;
  cursor: pointer;
  margin-left: 4px;
  transition: background 0.2s, color 0.2s;
}
.try-tag:hover {
  background: #4d8aff;
  color: #fff;
}
.input-float {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 0 40px 32px 40px;
  background: transparent;
  z-index: 10;
}
</style> 